package com.ruoyi.web.controller.pc.anon;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.blogs.domain.BsBrowseHistory;
import com.ruoyi.blogs.domain.BsComment;
import com.ruoyi.blogs.domain.BsNote;
import com.ruoyi.blogs.domain.vo.BsCommentVo;
import com.ruoyi.blogs.domain.vo.BsNoteVo;
import com.ruoyi.blogs.service.IBsBrowseHistoryService;
import com.ruoyi.blogs.service.IBsCommentService;
import com.ruoyi.blogs.service.IBsNoteService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "博客文章")
@RestController
@RequestMapping("/anon/note")
public class AnonNoteController extends BaseController {
    @Autowired
    private IBsNoteService bsNoteService;

    @Autowired
    private IBsCommentService bsCommentService;

    @Autowired
    private IBsBrowseHistoryService bsBrowseHistoryService;

    /**
     * 查询笔记管理列表
     */
    @ApiOperation("获取博客文章列表")
    @GetMapping("/list")
    public TableDataInfo list(BsNote bsNote) {
        startPage();
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("userId",SecurityUtils.getUserId());
            bsNote.setParams(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        bsNote.setVisible(0);
        List<BsNoteVo> list = bsNoteService.selectBsNoteVoList(bsNote);
        return getDataTable(list);
    }

    /**
     * 获取笔记管理详细信息
     */
    @ApiOperation("获取博客文章详情")
    @Transactional
    @GetMapping
    public R<BsNoteVo> getInfo(BsNote bsNote) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", bsNote.getId());
        // 更新浏览量
        bsNoteService.updateBsNoteViewById(bsNote);
        BsComment bsComment = new BsComment();
        bsComment.setNoteId(bsNote.getId());
        try {
            Long userId = SecurityUtils.getUserId();
            bsComment.setUserId(userId);
            map.put("userId", userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 查询文章详情
        BsNoteVo bsNoteVo = bsNoteService.selectBsNoteVoByQuery(map);
        try {
            // 添加历史记录
            BsBrowseHistory bsBrowseHistory = new BsBrowseHistory();
            bsBrowseHistory.setConnectId(bsNote.getId());
            bsBrowseHistory.setType(0);
            bsBrowseHistory.setTitle(bsNoteVo.getTitle());
            bsBrowseHistoryService.insertBsBrowseHistory(bsBrowseHistory);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 查询文章评论详情
        startPage();
        List<BsCommentVo> bsComments = bsCommentService.selectBsNoteCommentList(bsComment);
        TableDataInfo table = getDataTable(bsComments);
        bsNoteVo.setComments((List<BsCommentVo>) table.getRows());
        return R.ok(bsNoteVo);
    }
}
